package l1j.server.server.utils;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

public class TimeUtil {

	public static String CalendarToString(Calendar cal) {
		return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
				.format(cal.getTime());
	}

	public static String timeSecToString(int time) {
		StringBuilder sb = new StringBuilder();
		if (time > 3600) {
			sb.append(time / 3600);
			sb.append("小時 ");
			time %= 3600;
		}
		if (time > 60) {
			sb.append(time / 60);
			sb.append("分 ");
			time %= 60;
		}
		sb.append(time);
		sb.append("秒");
		return sb.toString();
	}

	// 從 SQL 的 Timestamp 轉換成日曆
	public static Calendar timestampToCalendar(Timestamp ts) {
		Calendar cal = getCalendar();
		cal.setTimeInMillis(ts.getTime());
		return cal;
	}

	// 從 SQL 的 Time 轉換成日曆
	public static Calendar timestampToCalendar(Time ts) {
		Calendar cal = getCalendar();
		cal.setTimeInMillis(ts.getTime());
		return cal;
	}

	/** 取得今天0點開始 **/
	public static Calendar getTodayStart() {
		TimeZone tz = TimeZone.getTimeZone("Asia/Taipei");
		Calendar cal = Calendar.getInstance(tz);
		cal.set(Year(), Month(), DayMonth(), 0, 0, 0);
		return cal;
	}

	public static Calendar getCalendar() {
		TimeZone tz = TimeZone.getTimeZone("Asia/Taipei");
		Calendar cal = Calendar.getInstance(tz);
		return cal;
	}

	public static int Year() {
		return Calendar.getInstance().get(Calendar.YEAR);
	}

	public static int Month() {
		return Calendar.getInstance().get(Calendar.MONTH);
	}

	public static int DayMonth() {
		return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 星期日：1 星期一：2 星期二：3 星期三：4 星期四：5 星期五：6 星期六：7
	 * */
	public static int DayWeek() {
		return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
	}

	/** 24小時制 */
	public static int Hour24() {
		return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
	}

	/** 12小時制 */
	public static int Hour12() {
		return Calendar.getInstance().get(Calendar.HOUR);
	}

	public static int Minute() {
		return Calendar.getInstance().get(Calendar.MINUTE);
	}

	public static int Second() {
		return Calendar.getInstance().get(Calendar.SECOND);
	}

	public static int Date() {
		return Calendar.getInstance().get(Calendar.DATE);
	}

}
